Count the number of lines in a text fileΒΆ
Count the number of lines in a text file.
def file_lengthy(fname):
with open(fname) as f:
for i, l in enumerate(f):
pass
return i + 1
# test
print("Number of lines in the file: ", file_lengthy("test.txt"))
Output:
Number of lines in the file: 6